package com.example.backend.controller.order;

import com.example.backend.dto.request.OrderDto;
import com.example.backend.enums.ResultCodeEnum;
import com.example.backend.service.mysql.order.OrderItemsService;
import com.example.backend.service.mysql.order.OrdersService;
import com.example.backend.utils.OrderNumberGenerator;
import com.example.backend.utils.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "订单管理", description = "订单管理接口")
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Validated
@Transactional(rollbackFor = Exception.class)
public class OrderController {
    private final OrdersService ordersService;

    private final OrderItemsService orderItemsService;

    public static Integer getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            return (Integer) authentication.getPrincipal();
        }
        throw new UsernameNotFoundException(ResultCodeEnum.NOT_LOGIN.getMessage());
    }

    @Operation(summary = "取消订单", description = "取消订单")
    @PostMapping("/cancel")
    @PreAuthorize("hasAnyAuthority('user:cancel')")
    public ResultJson<?> cancelOrder() {

        return null;
    }

    @Transactional
    @Operation(summary = "创建订单", description = "创建订单")
    @PostMapping("/create")
    @PreAuthorize("hasAnyAuthority('user:create')")
    public ResultJson<?> createOrder(@RequestBody OrderDto orderDto) {
        orderDto.setUserId(getUser());
        orderDto.setOrderNo(OrderNumberGenerator.generateOrderNumber());

        return null;
    }

    @Transactional
    @Operation(summary = "获取订单详情", description = "获取订单详情")
    @PostMapping("/detail")
    @PreAuthorize("hasAnyAuthority('user:detail')")
    public ResultJson<?> getOrderDetail() {
        return null;
    }

    @Transactional
    @Operation(summary = "获取订单列表", description = "获取订单列表")
    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('user:list')")
    public ResultJson<?> getOrderList() {
        return null;
    }

    @Transactional
    @Operation(summary = "修改订单信息", description = "修改订单信息")
    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('user:update')")
    public ResultJson<?> updateOrder() {
        return null;
    }

    @Transactional
    @Operation(summary = "删除订单", description = "删除订单")
    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('user:delete')")
    public ResultJson<?> deleteOrder() {
        return null;
    }
}
